<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 600px;
        }
        
        h3 {
            border-bottom: 1px solid #ccc;
        }
        
        textarea {
            width: 594px;
            height: 300px;
        }
        
        .btn {
            width: 600px;
            height: 50px;
        }
        
        .doc {
            border: 1px solid orange;
        }
        
        .btn:hover {
            background: skyblue;
        }
        
        .item {
            width: 100%;
            min-height: 50px;
            border: 1px dotted blue;
        }
    </style>
</head>

<body>
    <div class="box">
        <h3>留言内容</h3>
        <div class="doc"></div>
        <textarea name="" id="" cols="30" rows="10" placeholder="请输入留言内容"></textarea>
        <button class="btn">发表留言</button>
    </div>
</body>
<script>
    // 留言板
    // 获取元素
    let text_input = $("textarea")[0];
    let btn = $(".btn")[0];
    let doc = $(".doc")[0];
    // console.log(doc);
    // 统计留言的次数
    let index = 0;
    btn.addEventListener('click', function() {
        let text = text_input.value;
        let oP = document.createElement("p");
        oP.innerHTML = "第" + (index + 1) + "次留言:" + text + "<button>删除</button>";
        oP.className = "item";
        doc.appendChild(oP);
        index++;
        let delete_btn = doc.querySelectorAll('button');
        for (let i = 0; i < delete_btn.length; i++) {
            delete_btn[i].addEventListener('click', function() {
                doc.removeChild(this.parentNode);

            });
        }
        console.log(delete_btn);
        // 添加删除事件
        text_input.value = "";




    })

    function addEventListener(element, eventName, fn) {
        if (element.addEventListener) {
            element.addEventListener(eventName, fn);
        } else if (element.attachEvent) {
            element.attachEvent("on" + eventName, fn);
        } else {
            // 相当于element.onclick = fn;
            element['on' + eventName] = f(n);
        }
    }

    function $(select) {
        return document.querySelectorAll(select);
    }
</script>

</html>